import {expect} from "chai"
import {v1} from "uuid"
import {fromJS,Map,List} from "immutable"

import {
    addRoom,
    removeRoom
} from '../../src/server/core.js'


describe("rooms", ()=>{
    
    it("能够添加房间：addRoom", ()=>{
        var firstRoom = {name: "First room", id: v1(), owner: "eisneim"}
        const nextState = addRoom(undefined, firstRoom)
        const rooms = nextState.get("rooms");
        expect( rooms ).to.be.ok;
        expect( rooms.get(0) ).to.equal(Map(firstRoom))

        const nextNextState = addRoom(nextState, {
            name: "second room", owner: "terry"
        })
        expect( nextNextState.getIn(["rooms", 1, "owner"]) ).to.equal("terry")
    })

    // 测试数据
    const mockState = fromJS({
        rooms: [{name: "first room", id:v1(), owner: "owner1"}]
    })

    it("能够被创建者删除: removeRoom", ()=>{
        const state = removeRoom( mockState, {
            id: mockState.getIn(["rooms", 0, "id"]),
            user: "owner1"
        } )

        expect( state.get("rooms").size ).to.equal(0)
    })

    it("不能够被非创建者删除：removeRoom", ()=>{
        const state = removeRoom( mockState, {
            id: mockState.getIn(["rooms", 0, "id"]),
            user: "owner2"
        } )

        expect( state.get("rooms").size ).to.equal(1)
    })
})


